# fdk-aac android 编译 修复脚本

set(FDK_AAC_SOURCE_DIR "${SOURCE_DIR}")
set(LPP_TRAN_CPP "${FDK_AAC_SOURCE_DIR}/libSBRdec/src/lpp_tran.cpp")

if(NOT EXISTS "${LPP_TRAN_CPP}")
    message(FATAL_ERROR "❌ 文件不存在: ${LPP_TRAN_CPP}")
endif()

# 读取文件内容
file(READ "${LPP_TRAN_CPP}" CONTENTS)

# 替换 #include "log/log.h"
if("${CONTENTS}" MATCHES "#include \"log/log.h\"")
    string(REPLACE
        "#include \"log/log.h\""
        "#include <android/log.h>\n\n#define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, \"FDK-AAC\", __VA_ARGS__)\n"
        CONTENTS "${CONTENTS}"
    )
    message(STATUS "✅ 已替换 log/log.h → android/log.h")
else()
    message(WARNING "⚠️ 未找到 #include \"log/log.h\"，可能已修复")
endif()

# 替换 android_errorWriteLog
if("${CONTENTS}" MATCHES "android_errorWriteLog\\(0x534e4554, \"112160868\"\\);")
    string(REPLACE
        "android_errorWriteLog(0x534e4554, \"112160868\");"
        "ALOGE(\"lpp_tran error detected\");"
        CONTENTS "${CONTENTS}"
    )
    message(STATUS "✅ 已替换 android_errorWriteLog → ALOGE")
endif()

# 写回文件
file(WRITE "${LPP_TRAN_CPP}" "${CONTENTS}")

message(STATUS "🔧 fdk-aac 修复完成: ${LPP_TRAN_CPP}")